// pages/pingjia/pingjia.js
var api = require('../../utils/api.js')

var cf = require("../../config.js");
Page({
  data: {
    imagesList:[
          '../../images/icon_A.png'
    ],
    imagesLists:[],
    show:true,
    //品质
    quality:'',
    //服务
    serve:"",
    //时效
    prescription:"",
    //品质
    qualityRight:"",
    //服务
    serveRight:"",
    //时效
    prescriptionRight:"",

    //图片数组
    arrImgs:[],
    //评论内容
    content:'',
    //图片
    url:'',
    orderCode:'',
    goodsId:''
  },

  onLoad: function (options) {
   
    this.setData({
      url: options.goodsPic,
      goodsId: options.goodsId,
      orderCode: options.orderCode
    })
   
  },

  //品质
  onChange:function(e){
    let that=this
    //获取品质评分评价
    let qualityRight = that.data.qualityRight
    if (e.detail<2){
        qualityRight='非常差'
    } else if (3 > e.detail && e.detail> 1){
       qualityRight = '差'
    } else if (4> e.detail && e.detail >2) {
      qualityRight = '一般'
    } else if (5 > e.detail && e.detail > 3) {
      qualityRight = '好'
    } else if (6 > e.detail && e.detail > 4) {
      qualityRight = '非常好'
    }

    that.setData({
      quality: e.detail,
      qualityRight: qualityRight
    })

  },

  //服务
  chengServe:function(e){
    let that = this
    //获取服务评分评价
    let serveRight = that.data.serveRight
    if (e.detail < 2) {
      serveRight = '非常差'
    } else if (3 > e.detail && e.detail > 1) {
      serveRight = '差'
    } else if (4 > e.detail && e.detail > 2) {
      serveRight = '一般'
    } else if (5 > e.detail && e.detail > 3) {
      serveRight = '好'
    } else if (6 > e.detail && e.detail > 4) {
      serveRight = '非常好'
    }
    that.setData({
      serve: e.detail,
      serveRight: serveRight
    })
  },

  //时效
  changePrescription:function(e){
    let that = this
    //获取服务评分评价
    let prescriptionRight = that.data.prescriptionRight
    if (e.detail < 2) {
      prescriptionRight = '非常差'
    } else if (3 > e.detail && e.detail > 1) {
      prescriptionRight = '差'
    } else if (4 > e.detail && e.detail > 2) {
      prescriptionRight = '一般'
    } else if (5 > e.detail && e.detail > 3) {
      prescriptionRight = '好'
    } else if (6 > e.detail && e.detail > 4) {
      prescriptionRight = '非常好'
    }
    that.setData({
      prescription: e.detail,
      prescriptionRight: prescriptionRight
    })
  },
   
  //上传图片
  addImg:function(){
    let that=this
    wx.chooseImage({
      count:3,
      success(res) {
        var tempFilePaths = res.tempFilePaths;
        if (res.errMsg == "chooseImage:ok"){
          that.setData({
            imagesList: res.tempFilePaths,
            imagesLists: res.tempFilePaths,
            show:false,
            arrImgs: []
          }) 
          //调用上传图片 url 数组图片
          that.uploadFiles(`${cf.url}/admin/goodsEvalEntity/uploadFile`, res.tempFilePaths)
        }
      }
    })
  },

  //删除图片
  deleteImages:function(e){
    let that=this
    if (that.data.imagesList.length==1){
      that.setData({
          imagesList: ['../../images/icon_A.png'],
          show: true
       })
    }else{
      that.setData({
        imagesList: that.data.imagesList.splice(0,e.currentTarget.dataset.index),
        imagesLists: that.data.imagesLists.splice(0, e.currentTarget.dataset.index)
      })
      if (that.data.imagesList.length == 0) {
        that.setData({
          imagesList: ['../../images/icon_A.png'],
          show: true
        })
      }
    }
  },

  //提交
  handleSubmit:function(e){
 console.log(e)
    let that=this
    if (that.data.quality==""){
      wx.showToast({
        title: '请评论品质',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (that.data.serve == "") {
      wx.showToast({
        title: '请评论服务',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (that.data.prescription == "") {
      wx.showToast({
        title: '请评论时效',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (e.detail.value.evaluate == "") {
      wx.showToast({
        title: '请填写评论内容',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (that.data.imagesLists.length == 0) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    api.saveEval({
      aging: that.data.prescription, //时效
      content: e.detail.value.evaluate,//评论内容
      createid: wx.getStorageSync('openId'), //用户id     
      serve: that.data.serve,//服务
      quality: that.data.quality,//品质
      goodsEvalPictureEntityList: that.data.arrImgs,
      status:2,
      goodsId: that.data.goodsId,//商品id
      orderCode: that.data.orderCode,//订单id
    }).then(res => {
      if (res.result){
        wx.showToast({
          title: '评价成功',
          icon: "none",
          duration: 1000,
          success: () => {
            setTimeout(function () {
              wx.navigateBack({
                data: 3
              })
            }, 1000)
          }
        })
      }
    })
  },


  //图片上传服务器
  uploadFiles:function(url,arrImg){
    let that=this;
    let arrImgs = that.data.arrImgs
    let num=0
    //遍历数组每张图片
    arrImg.forEach(function(v){
      wx.uploadFile({
        url: url,
        filePath: v,
        name: 'file',
        success(res) {
     
          res = JSON.parse(res.data)
        
          if (res.result){
            arrImgs.push({
                url: res.object,
                createid: wx.getStorageSync('openId'),
                fileType: 1,
                goodsId: that.data.goodsId,
                orders: ++num
              })
          }else{
            wx.showToast({
              title: '上传失败',
              icon: "none",
            })
          }
        }
      })
    })
      that.setData({
        arrImgs:arrImgs
      })
  }
})


